function iNotify(config) {
    if (config) {
        this.init(config);
    }
}
iNotify.prototype = {
    init: function(config) {
        if (!config) {
            config = {};
        }
        this.audio = config.audio || '';
        //初始化生成声音文件节点
        if (this.audio) {
            //循环设置

            for (var item in this.audio) {
                this.setURL(item, this.audio[item]);
            }
        }
        return this;
    },
    setURL: function(id, audioObj) {

        if (audioObj.file) {
            if (audioObj['audioElm']) {
                audioObj['audioElm'].remove();
            }

            audioObj['audioElm'] = createAudio(id, audioObj['file']);
            document.body.appendChild(audioObj['audioElm']);
        }
        return this;
    },
    //播放声音
    player: function(key) {
        var adi = this.audio[key],
            source = null;
        if (!this.audio || !this.audio[key]) {
            return;
        }
        if (!this.audio[key]['audioElm']) {
            this.audio[key]['audioElm'] = createAudio(key, this.audio[key]['file']);
            document.body.appendChild(this.audio[key]['audioElm'])
        }
        // this.audio[key]['audioElm'].play();
        document.getElementById(key).play();
        return this;
    }



};

function createAudio(id, url) {
    var audioElm = document.createElement('audio'),
        source;
    audioElm.id = id;
    if (isArray(url) && url.length > 0) {
        for (var i = 0; i < url.length; i++) {
            source = document.createElement('source');
            source.src = url[i];
            source.type = 'audio/' + getExtension(url[i]);
            audioElm.appendChild(source);
        }
    } else {
        audioElm.src = url;
    }
    return audioElm;
}

function isArray(value) {
    return Object.prototype.toString.call(value) === '[object Array]';
}


//获取文件后缀
function getExtension(file_name) {
    return file_name.match(/\.([^\.]+)$/)[1];
}


var iNotify2 = new iNotify({

    audio: {

        printOk: {
            audioElm: null,
            file: require('./printok.mp3')
        },
        printError: {
            audioElm: null,
            file: require('./printerror.mp3')
        },
        signOk: {
            audioElm: null,
            file: require('./ok.wav')
        },
        signDouble: {
            audioElm: null,
            file: require('./double.wav')
        },
        signError: {
            audioElm: null,
            file: require('./error.wav')
        },
        printSmall: {
            audioElm: null,
            file: require('./printsmall.wav')
        },
         printBig: {
            audioElm: null,
            file: require('./printbig.wav')
        },
         printOut: {
            audioElm: null,
            file: require('./printout.wav')
        }
    }
});
export default iNotify2;